package gui;
import db.DBManager;
import javax.swing.*;
import java.util.*;

/**
 * A class to reactivate a deactivated Ministry.
 * Data will not be editable from this window. You must edit an
 * activated ministry.
 *
 * @author Sarah Wegner & Gabrielle DuBois
 */
public class ReactivateMinistry extends javax.swing.JFrame {

    /** Creates new form ReactivateMinistry */
    public ReactivateMinistry() {
        initComponents();
        initFields();
    }//ReactivateMinistry() Constructor

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rbNo = new javax.swing.JRadioButton();
        rbYes = new javax.swing.JRadioButton();
        jSeparator2 = new javax.swing.JSeparator();
        lblAddNewHousehold = new javax.swing.JLabel();
        btCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtNotes = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        btActivate = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbMinistryName = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        rbNo.setText("No");
        rbNo.setEnabled(false);

        rbYes.setText("Yes");
        rbYes.setEnabled(false);

        jSeparator2.setForeground(new java.awt.Color(0, 153, 0));

        lblAddNewHousehold.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblAddNewHousehold.setText("Reactivate Ministry");

        btCancel.setText("Cancel");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        txtNotes.setColumns(20);
        txtNotes.setRows(5);
        txtNotes.setEnabled(false);
        jScrollPane1.setViewportView(txtNotes);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Notes:");

        btActivate.setText("Activate");
        btActivate.setMaximumSize(new java.awt.Dimension(65, 23));
        btActivate.setMinimumSize(new java.awt.Dimension(65, 23));
        btActivate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btActivateActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Ministry Name:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Is this ministry a Community Service Ministry?");

        cbMinistryName.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbMinistryNameItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAddNewHousehold)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addComponent(jLabel3)
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(29, 29, 29)
                            .addComponent(rbYes, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                            .addGap(82, 82, 82)
                            .addComponent(rbNo, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addComponent(cbMinistryName, 0, 341, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(btCancel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btActivate, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)))
                .addGap(29, 29, 29))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblAddNewHousehold)
                .addGap(11, 11, 11)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbMinistryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(rbYes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(rbNo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(101, 101, 101))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btActivate, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        int answer = JOptionPane.showConfirmDialog(null, "Are you sure "+
                "you would like to close "+ "\n"+ "this window without saving?",
                "Cancel", JOptionPane.YES_NO_OPTION);
        if (answer == JOptionPane.YES_OPTION) {
            // User clicked YES.
            this.dispose();
        } //else if (answer == JOptionPane.NO_OPTION)
        // User clicked NO. Do nothing.
}//GEN-LAST:event_btCancelActionPerformed

    private void btActivateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btActivateActionPerformed
        activate(cbMinistryName.getSelectedItem().toString());
}//GEN-LAST:event_btActivateActionPerformed

    private void cbMinistryNameItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbMinistryNameItemStateChanged
        try{
            DBManager dbManager = new DBManager();

            //get ministry and set other fields
            String[] ministrySelected = dbManager.getMinistry((String)evt.getItem());
            if(ministrySelected[1].equals("false")){
                rbNo.setSelected(true);
                rbYes.setSelected(false);
            }
            else{
                rbYes.setSelected(true);
                rbNo.setSelected(false);
            }
            txtNotes.setText(ministrySelected[3]);

         }//try
         catch(Exception ex){
            JOptionPane.showMessageDialog(null, "An error has occurred." + "\n" +
                    "Please contact your Database Administrator, a.k.a Thomas Naps, for help." + "\n" +
                    "(cbMinistryNameItemStateChanged(evt) Method)",
                    "Database Error", JOptionPane.ERROR_MESSAGE);
         }//catch
    }//GEN-LAST:event_cbMinistryNameItemStateChanged

    /**
     * Set Drop-down of Ministry Name Drop-down to all
     * non-active ministry names
     */
    private void initFields(){
         try{
            DBManager dbManager = new DBManager();
            ArrayList<String> ministryNames = dbManager.getMinistryNames(false);
            String[] ministryData = new String[4];//array to populate other fields per selected ministry

            for(int i = 0; i < ministryNames.size(); i++){
                cbMinistryName.addItem(ministryNames.get(i));
            }//for-i each ministry Name Selected

            //get ministry and set other fields
            ministryData = dbManager.getMinistry(ministryNames.get(0));
            if(ministryData[1].equals("false")){
                rbNo.setSelected(true);
            }
            else{
                rbYes.setSelected(true);
            }
            txtNotes.setText(ministryData[2]);

         }//try
         catch(Exception ex){
            JOptionPane.showMessageDialog(null, "An error has occurred." + "\n" +
                    "Please contact your Database Administrator, a.k.a Thomas Naps, for help." + "\n" +
                    "(ReactivateMinistry.java: initFields() Method)",
                    "Database Error", JOptionPane.ERROR_MESSAGE);
         }//catch
    }//initfields()

    /**
     * A method to activate a ministry
     *
     * @param String ministryName - the name of a ministry in string form
     * to be passed to the DBManager method toggleMinistryActivationStatus(String,Boolean)
     */
    public void activate(String ministryName){
        //call toggle ministry
        try{
            DBManager dbManager = new DBManager();
            dbManager.toggleMinistryActivationStatus(ministryName, true);
            JOptionPane.showMessageDialog(null, ministryName +
                    " is now active.", "Activate", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }//try
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, "An error has occurred." + "\n" +
                    "Please contact your Database Administrator, a.k.a Thomas Naps, for help." + "\n" +
                    "(activate(String) Method)",
                    "Database Error", JOptionPane.ERROR_MESSAGE);
        }//catch
    }//actiavate

    /**
     * Will open the gui if there are inactive ministries
     * Otherwise a message will appear.
     */
    public static void run() {
        try{
            DBManager dbManager = new DBManager();
            ArrayList<String> ministryNames = dbManager.getMinistryNames(false);
            if(ministryNames.isEmpty()){
                JOptionPane.showMessageDialog(null, "There are no inactive" +
                        " ministries at this time.",
                        "Reactivate Ministry",
                        JOptionPane.INFORMATION_MESSAGE);
            }//if
            else{
                new ReactivateMinistry().setVisible(true);
            }//else

        }//try
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, "An error has occurred." + "\n" +
                    "Please contact your Database Administrator, a.k.a Thomas Naps, for help." + "\n" +
                    "(ReactivateMinistry.java: run() Method)",
                    "Database Error", JOptionPane.ERROR_MESSAGE);
        }//catch
    }//run

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btActivate;
    private javax.swing.JButton btCancel;
    private javax.swing.JComboBox cbMinistryName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblAddNewHousehold;
    private javax.swing.JRadioButton rbNo;
    private javax.swing.JRadioButton rbYes;
    private javax.swing.JTextArea txtNotes;
    // End of variables declaration//GEN-END:variables

}//ReactivateMinistry
